package com.gin.no_gay_alliance.viewmodel.notice;

import android.os.Parcel;
import android.os.Parcelable;

import com.gin.common.utils.TimeUtils;
import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.response.field.notice.BaseNotice;

import java.util.Collection;

/**
 * 提醒消息基类
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/11 10:23
 **/
public class BaseNoticeModel<T extends BaseNotice> implements Parcelable {
    public static final Creator<BaseNoticeModel> CREATOR = new Creator<BaseNoticeModel>() {
        @Override
        public BaseNoticeModel createFromParcel(Parcel in) {
            return new BaseNoticeModel(in);
        }

        @Override
        public BaseNoticeModel<?>[] newArray(int size) {
            return new BaseNoticeModel[size];
        }
    };
    /**
     * 时间戳
     */
    public String datetime;
    public long timestamp;
    /**
     * 是否已读
     */
    public boolean read;
    /**
     * 描述
     */
    public String description;

    public BaseNoticeModel() {
    }


    public BaseNoticeModel(T baseNotice) {
        this.timestamp = baseNotice.getTimestamp().toEpochSecond();
        this.datetime = TimeUtils.format(baseNotice.getTimestamp(), TimeUtils.SHORT_FORMATTER);
        this.read = false;
        this.description = baseNotice.getDescription();
    }

    protected BaseNoticeModel(Parcel in) {
        datetime = in.readString();
        read = in.readByte() != 0;
        description = in.readString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(datetime);
        dest.writeByte((byte) (read ? 1 : 0));
        dest.writeString(description);
    }

    /**
     * 统计一个列表中的未读数量
     *
     * @param collection 合集
     * @return 未读数
     */
    public static long countUnread(Collection<? extends BaseNoticeModel<?>> collection) {
        if (ObjectUtils.isEmpty(collection)) {
            return 0;
        }
        return collection.stream().filter(i -> !i.read).count();
    }

    /**
     * 设置全部已读
     *
     * @param collection 合集
     */
    public static void readAll(Collection<? extends BaseNoticeModel<?>> collection) {
        if (!ObjectUtils.isEmpty(collection)) {
            collection.forEach(i -> i.read = true);
        }
    }
}
